package com.fs.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Calendar;
import java.util.Date;

public class JWTDemo {
    @Test
    public void testCreateToken(){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND,90);
        JwtBuilder builder = Jwts.builder().setId("888").setSubject("zhangsan")
                .setIssuedAt(new Date())//设置签发时间
                .signWith(SignatureAlgorithm.HS256, "feisi")//使用HS256生成token,签名秘钥 则是feisi，唯一密钥的话可以保存在服务端。
                .setExpiration(instance.getTime());//设置过期时间
        System.out.println(builder.compact());
    }

    @Test
    public void testProcessToke(){
        String token ="eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiJ6aGFuZ3NhbiIsImlhdCI6MTcwMzQ5Nzk4NywiZXhwIjoxNzAzNDk4MDc3fQ.q9vRvMGHsMsVPSV7rgr2_hQ0_act9jPlsRM5tr4_FgQ";
        Claims claims =
                Jwts.parser().setSigningKey("feisi").parseClaimsJws(token).getBody();
        System.out.println("id:"+claims.getId());
        System.out.println("subject:"+claims.getSubject());
        System.out.println("IssuedAt:"+claims.getIssuedAt());
        System.out.println("expiration:"+claims.getExpiration());
    }
}
